/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ChateoController;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

/**
 *
 * @author adrian
 */
public class UsersList extends ArrayList<User>{
    //TODO : Faire une recherche par Login et IP qui retourne un id, par id
        
    private User localUser;
    public UsersList(){  }
    public UsersList(User localUser) throws UnknownHostException {
        String transfert=localUser.getAddr().getHostAddress();
        
		this.localUser = localUser;
                this.localUser.setAddr(InetAddress.getByName(transfert));
                        this.add(localUser);
	}

	/**
	 *This method is aimed at adding a new user the user list
        * @param user is the user that is going to be added to the list
	 */
	public void addUser(User user) {
		this.add(user);
	}

	/**
	 * This method is aimed at removing a user from the user list
	 * @param user is the user that is going to be removed to the list.
	 */
	public void removeUser(User user) {
		this.remove(user);
	}

	/**
	 * Get local user.
	 * @return localUser Local user.
	 */
	public User getLocalUser() {
		return localUser;
	}

	/**
	 * Set localu user.
	 * @param localUser New local user
	 */
	public void setLocalUser(User localUser) {
		this.localUser = localUser;
	}

	/**
	 * Get a specific user from the list.
	 * @param user User to return.
	 * @return user The real user from the list.
	 */
	public User get(User user) {
		return this.get(this.indexOf(user));
	}

	/**
	 * Get local user name.
	 * @return name The name of the local user.
	 */
	public String getLocalUsername() {
		return localUser.getLogin();
	}
        
        public boolean contains(User user){
            boolean bool=false;
            int size = this.size();
            for (int i=0 ; i<size ;i++){
                if (Controller.DebugMode()){System.out.println("Test : "+i); };
                if(user.equals(this.get(i))){
                    bool=true;
                }
                //bool=((user.getLogin().equals(this.get(i).getLogin()))&&(user.getAddr().getHostAddress().equals(this.get(i).getAddr().getHostAddress()))&&(user.isLoggedIn()==this.get(i).isLoggedIn()));
            }
            return bool;
        }
        public int indexOf(User user){
            int dummy=-1;
            int size = this.size();
            for (int i=0 ; i<size ;i++){
                
                if(user.equals(this.get(i))){
                    dummy=i;
                }
                else{
                    
                }
            }
            return dummy;
        }
         public User getUserByLogin(String login){
            User dummy=localUser;
            int size = this.size();
            for (int i=0 ; i<size ;i++){
                
                if(login.equals(this.get(i).getLogin())){
                    dummy=this.get(i);
                }
                else{
                    
                }
            }
            return dummy;
        }
         public ArrayList<User> getConnectedUsers(){
             ArrayList<User> connectedUsers=new ArrayList<User>();
             
             int size = this.size();
             if (size>0){
             int j=0;
             while (this.get(j).getLogin().equals(localUser.getLogin())){
                 j++;
             }
             
            for (int i=j ; i<size ;i++){
                if(this.get(i).isLoggedIn()){
                connectedUsers.add(this.get(i));
                }
            }
             }
             return connectedUsers;
         }
}
